/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved.
 * Description: 上机编程认证
 * Note: 缺省代码仅供参考，可自行决定使用、修改或删除
 */
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    // 待实现函数，在此函数中填入答题代码
    int GetMaxSendNum(int cap, const vector<int> &bill, const vector<int> &pri) {
        /*首先对优先级进行排序，得到其索引的排序*/
        vector<int> idx(bill.size());
        //初始化索引
        for (int i = 0; i < bill.size(); ++i) {
            idx[i] = i;
        }
        //排序
        sort(idx.begin(), idx.end(), [&pri,&bill](int a, int b)
            {
            if (pri[a] == pri[b]) {
                return bill[a] < bill[b];
            }
            return pri[a] < pri[b];
            }
        );

        int i = 0;
        while (i < idx.size() and cap >= 0) {
            cap -= bill[idx[i]];
            i++;
        }
        return cap >= 0 ? i : i - 1;
    }
};

// 以下为考题输入输出框架，此部分代码不建议改动
inline int ReadInt() {
    int number;
    std::cin >> number;
    return number;
}

template<typename T>
inline std::vector<T> ReadVector(int size) {
    std::vector<T> objects(size);
    for (int i = 0; i < size; ++i) {
        std::cin >> objects[i];
    }
    return objects;
}

int main() {
    int cap = ReadInt();
    int n = ReadInt();
    vector<int> bill = ReadVector<int>(n);
    vector<int> pri = ReadVector<int>(n);
    Solution solu;
    int res = solu.GetMaxSendNum(cap, bill, pri);
    cout << res;
    return 0;
}
